<?php
/**
 * 刘红伟 2020-09-28
 */


namespace App\Services\Front;


use App\Http\Controllers\Controller;
use App\Models\Config;
use App\Models\SysConfig;
use Illuminate\Http\Request;
use App\Libs\httpUtil;

class WxService
{

    /**
     * 获取accessToken
     * @return bool
     */
    public function getAccessToken()
    {
        $data = \RedisDB::get(env("APP_NAME") . '-access_token');
        if ($data) {
            $accessToken = json_decode($data, true);
            return $accessToken['access_token'];
        } else {
            $accessToken = $this->refreshAccessToken();
            return $accessToken;
        }
    }

    /**
     * 刷新accessToken
     * @return bool
     */
    private function refreshAccessToken()
    {
        $config = SysConfig::where('id','!=',0)->first();
        $appId = $config->appid;
        $appSecret = $config->appsecret;
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appId . "&secret=" . $appSecret;
        $http = new httpUtil();
        $data = $http->http_get($url);
        if (!$data) {
            return false;
        }
        $result = json_decode($data, true);
        if (!empty($result['errcode'])) {
            \Log::error('WxService  refreshAccessToken' . $data);
            return false;
        }
        $result['createTime'] = date("Y-m-d H:i:s", time());

        \RedisDB::setex(env("APP_NAME") . '-access_token', 600, json_encode($result));
        return $result['access_token'];
    }

    /**
     * 获取accessToken
     * @return bool
     */
    public function getMZAccessToken()
    {
        $accessToken = $this->refreshMZAccessToken();
        $data = \RedisDB::get("MZ-APP" . '-access_token');
        if ($data) {
            $accessToken = json_decode($data, true);
            return $accessToken['access_token'];
        } else {
            $accessToken = $this->refreshMZAccessToken();
            return $accessToken;
        }
    }

    /**
     * 刷新accessToken
     * @return bool
     */
    private function refreshMZAccessToken()
    {
        $appId = "wxc823f0c1f2dd8d12";
        $appSecret = "0a39fb35db259e52af4ef8f971450d40";
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appId . "&secret=" . $appSecret;
        $http = new httpUtil();
        $data = $http->http_get($url);
        if (!$data) {
            return false;
        }
        $result = json_decode($data, true);
        if (!empty($result['errcode'])) {
            \Log::error('WxService  refreshMZAccessToken' . $data);
            return false;
        }
        $result['createTime'] = date("Y-m-d H:i:s", time());
        \RedisDB::setex("MZ-APP" . '-access_token', 600, json_encode($result));
        return $result['access_token'];
    }

    /**
     * 字符串内容验证
     * @return [type] [description]
     */
    public function stringVerify($content)
    {
        $access_token = $this->getAccessToken();
        $data=json_encode(array('content'=>$content),JSON_UNESCAPED_UNICODE);
        $url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=".$access_token;
        $http = new httpUtil();
        $data = $http->http_post($url,$data);
        \Log::error($data);
        if (!$data) {
            return $this->apiResponse('', config('errorCode.SHIPINGHAOBUDAOLEAIYA'));
        }
        $data = json_decode($data);
        // $access_token = $data->access_token;
        return $data->errcode;
    }
}